package com.Controller;

import com.Service.TblempService;
import com.Tool.Msg;
import com.domain.TblEmp;
import com.domain.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class TblempController {
    @Autowired
    private TblempService tblempService;

    @RequestMapping("selecttblemp")
    public String select(){
        List<TblEmp> tblEmpList = tblempService.select();
        for (TblEmp tblEmp:tblEmpList
        ) {
            System.out.println(tblEmp);
        }
        return "success";
    }

    @RequestMapping("selectwithdept")
    public String selectdept(){
        List<TblEmp> tblEmpList = tblempService.selectdept();
        for (TblEmp tblEmp:tblEmpList
        ) {
            System.out.println(tblEmp);
        }
        return "success";
    }

    @RequestMapping("batchinsert")
    public String batchinsert(){
        tblempService.batchinsert();
        return "success";
    }

    @RequestMapping("pagelist")
    public String pagelist(@RequestParam(value = "pn",defaultValue = "1")Integer pn,Model model){
        PageHelper.startPage(pn,10);
        List<TblEmp> tblEmpList = tblempService.selectdept();
        PageInfo pageInfo = new PageInfo(tblEmpList);
        model.addAttribute("pageinfo",pageInfo);
        return "list";
    }
    @RequestMapping("emps1")
    @ResponseBody
    public Msg msglist(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,10);
        List<TblEmp> tblEmpList = tblempService.selectdept();
        PageInfo pageInfo = new PageInfo(tblEmpList);
        return Msg.success().add("page",pageInfo);
    }

    @RequestMapping("del")
    @ResponseBody
    public Msg deluser(@PathVariable(value = "empId")String empId){
        //批量删除
        if(empId.contains("-")){
            List<Integer> del_id = new ArrayList<>();
            String [] str_ids = empId.split("-");
            for(String string:str_ids){
                del_id.add(Integer.parseInt(string));
            }
            tblempService.delbatch(del_id);
            return Msg.success();
        }
        else {
            int id = Integer.parseInt(empId);
            tblempService.deluser(id);
            return Msg.success();
        }


    }

    @RequestMapping("add")
    public String adduser(TblEmp tblEmp){
        tblempService.adduser(tblEmp);
        return "redirect:/emps1";
    }
    @RequestMapping("selbyid")
    @ResponseBody
    public Msg selbyid(@RequestParam(value = "empId")int empId){
        TblEmp emp = tblempService.selbyid(empId);
        return Msg.success().add("emp",emp);
    }

    @RequestMapping("upd")
    public String upduser(TblEmp tblEmp){
        tblempService.updbyid(tblEmp);
        return "redirect:/emps1";
    }
}
